const util = require('util')
import Axios from 'axios'
// const { User } = require('../models/User')
import Utils from '../../common/utils/Utils'
import Auth from '../../middlewares/Auth'
import httpErrors from '../../core/HttpException'
import config from '../../config/config'
import {Models} from '../../common/typings/model'
import UserManager from '../UserManager'
class WxManager {
    public static async codeToToken(code: string) {
        const url = util.format(config.wx.loginUrl,config.wx.appId,config.wx.appSecret,code)
        const result = await Axios.get(url)
        if(result.status !== 200) {
            throw new httpErrors.AuthFailed('openid获取失败')
        }
        const errcode = result.data.errcode
        if(errcode && errcode !== 0) {
            throw new httpErrors.AuthFailed('openid获取失败：'+errcode)
        }
        let user = await UserManager.getUser(result.data.openid)
        // if(!user) {
        //     user = await WxManager.register(result.data.openid)
        // }
        // let token = Utils.generateToken(user.id,Auth.USER)
        // return token 
    }
    // public static getUserByOpenid(openId: string): Models.User{

    //     return 
    // }
    // public static registerByOpenid(openId: string){
    //     return
    // }

}

export default WxManager
